package com.example.demo.controller;

import com.example.demo.config.ExceptionAdvice;
import com.example.demo.model.BlogInfo;
import com.example.demo.service.BlogService;
import com.example.demo.utils.JwtUtils;
import io.jsonwebtoken.lang.Assert;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.catalina.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/blog")
public class BlogController  {

    @Autowired
    private BlogService blogService;

    @Autowired
    private static Logger log = LoggerFactory.getLogger(BlogController.class);


    @RequestMapping("/getList")
    public List<BlogInfo> getList()
    {
        List<BlogInfo> list = blogService.getList();
        return list;
    }

    //博客详情页
    @RequestMapping("/getBlogDetail") //根据id获取博客详情
    public BlogInfo getBlogDetail(Integer blogId,HttpServletRequest request)
    {
        log.info("addBlog接收参数:"+blogId); //打印日志
        if(blogId == null)
        {
            throw new UnsupportedOperationException("id参数不能为null");
        }
        else if(blogId < 0)
        {
            throw new UnsupportedOperationException("id参数大于0");
        }
        BlogInfo blogInfo =  blogService.getBlogDetail(blogId);

        //作者ID
        Integer authorId = blogInfo.getUserId();
        String userToken = request.getHeader("user_token_header");
        Integer userId = JwtUtils.getUserIdFromToken(userToken);
        if(authorId != null &&  authorId == userId)
            blogInfo.setLoginUser(true);
        else
            blogInfo.setLoginUser(false);
        return blogInfo;
    }

    //发布博客
    @RequestMapping("/add")
    public boolean addBlog(@RequestBody BlogInfo blogInfo, HttpServletRequest request)
    {
        log.info("addBlog接收参数:" + blogInfo); //打印日志
        String userToken = request.getHeader("user_token_header");
        Integer userId = JwtUtils.getUserIdFromToken(userToken);
        Assert.notNull(userId,"userId不能为null");
        blogInfo.setUserId(userId);
        try {
            Integer result = blogService.insertBlog(blogInfo);
            if(result == 1)
            {
                return true;
            }
        }catch(Exception e){
            log.error("发布博客失败,e:"+e);
        }
            return false;
    }


    //更新博客
    @RequestMapping("/update")
    public Boolean updateBlog(@RequestBody BlogInfo blogInfo)
    {
        Integer result =  blogService.update(blogInfo);

        return result >= 1 ? true : false;
    }

    //删除博客
    @RequestMapping("/delete")
    public Boolean deleteBlog(Integer blogId)
    {
        Assert.notNull(blogId,"删除的博客ID不能为NULL");
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        Integer result = blogService.update(blogInfo);
        if(result >= 1)
            return true;
        return false;
    }


}
